package com.base.cn.platform.os.controller.order.user;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.service.manage.order.goods.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 赠送商品Controller
 *
 * @author s.li
 * @create 2018-05-22-10:49
 */
@Controller
@RequestMapping("/admin/order/user")
public class AdminUserGiveOrderController extends AdminBaseController {
    @Autowired
    private OrderService orderService;

    /**
     * 赠送用户课程
     * @param userId 用户ID
     * @param courseIds 课程ID
     * @return Map<String,Object>
     */
    @RequestMapping("/giveUserCourse")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.USER, action = "赠送用户课程")
    public Map<String,Object> giveUserCourse(@RequestParam("userId")BigDecimal userId,
                                             @RequestParam("courseIds") String courseIds){
        courseIds = StringUtils.subHeadTailString(courseIds,",");
        String[] arr = courseIds.split(",");
        //课程商品数据包
        String shopData = Arrays.stream(arr).map(e->e).collect(Collectors.joining("-COURSE-1,","","-COURSE-1"));
        return orderService.createFreeOrder(userId,shopData);
    }
}
